﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Runtime.Serialization;
using System.Data.Entity;

namespace CustomerService
{
    //public partial class SalesManagerEntities : DbContext
    //{
    //    private codeState[] _cachedStates = null;
    //    private codeCountry[] _cachedCountries = null;
    //    private codeLanguage[] _cachedLanguages = null;

    //    public codeState[] CachedStates
    //    {
    //        get
    //        {
    //            if (_cachedStates == null)
    //                _cachedStates = (from c in codeState
    //                                 select c).ToArray();
    //            return _cachedStates;
    //        }
    //    }

    //    public codeCountry[] CachedCountries
    //    {
    //        get
    //        {
    //            if (_cachedCountries == null)
    //                _cachedCountries = (from c in codeCountry
    //                                 select c).ToArray();
    //            return _cachedCountries;
    //        }
    //    }

    //    public codeLanguage[] CachedLanguages
    //    {
    //        get
    //        {
    //            if (_cachedLanguages == null)
    //                _cachedLanguages = (from c in codeLanguage
    //                                    select c).ToArray();
    //            return _cachedLanguages;
    //        }
    //    }
    //}

    public partial class tblPerson 
    {
        partial void Initialize()
        {
            FirstName = string.Empty;
            LastName = string.Empty;
            LanguageID = 1;
            //LanguageReference = new System.Data.Objects.DataClasses.EntityReference<codeLanguage>() { EntityKey = new System.Data.EntityKey("SalesManagerEntities.codeLanguage", "LanguageID", 1) };
        }
    }

    public partial class tblAddress 
    {
        partial void Initialize()
        {
            Address1 = string.Empty;
            CountryID = 1;
            //CountryReference = new System.Data.Objects.DataClasses.EntityReference<codeCountry>() { EntityKey = new System.Data.EntityKey("SalesManagerEntities.codeCountry", "CountryID", 468) };
        }
    }

    public partial class tblCustomer 
    {
        //public tblOrderHeader[] cachedOrderHeaders;
        //public tblOrderDetail[] cachedOrderDetails;
        //public tblContactPoint[] cachedContactPoints;
        //public tblSequenceOrder[] cachedSequenceOrders;
        //public tblSequenceOrderStep[] cachedSequenceOrderSteps;
        //public tblRefund[] cachedRefunds;
        //public tblRMAReturn[] cachedRMAReturns;
        //public tblReturnDetail[] cachedReturnDetails;
        //public tblTransaction[] cachedTransactions;
        //public tblMiscDef[] cachedMiscDefs;

        partial void Initialize()
        {
            ImportDate = DateTime.Now;
            AllowCleaning = true;
            AllowEmail = true;
            AllowMail = true;
            AllowPhone = true;
            AllowRent = true;
            DoNotProcess = false;
        }
    }

    public partial class tblOrderHeader 
    {
        partial void Initialize()
        {
            OrderDate = DateTime.Today;
            ProjShipDate = DateTime.Today;
            ImportDate = DateTime.Today;
            ProductTotal = 0;
            Shipping = 0;
            Taxes = 0;
            OrderTotal = 0;
        }

        public string OrderListInfo
        {
            get
            {
                string ret =
                    OrderDate.ToShortDateString() + "<br />" +
                    OrderStatus.Name;
                // Put the first shipment date into the list info
                //if (OrderStatus.OrderStatusCode == "S" || OrderStatus.OrderStatusCode == "R")
                //    ret += "<br />" + ((DateTime)OrderShipments.OrderBy(o => o.Shipment.ShippedDate).First().Shipment.ShippedDate).ToShortDateString();

                return ret;
            }
        }

        public bool CanEdit
        {
            get { return (OrderStatusCode == "P" || OrderStatusCode == "H"); }
        }

        public bool CanCancel
        {
            get { return (OrderStatusCode == "P" || OrderStatusCode == "H" || OrderStatusCode == "E"); }
        }

        public bool CanRefund
        {
            get { return true; }
        }
    }

    public partial class tblOrderDetail 
    {
        public decimal TotalPrice
        {
            get { return Convert.ToDecimal(Qty * (Convert.ToDouble(UnitPrice) * (1 - Discount))); }
        }
    }

    public partial class tblRefund 
    {
        public string LineDescription
        {
            get
            {
                if (OrderDetailID != null)
                    return OrderDetail.Product.ShortName;
                if (OrderTaxID != null)
                    return OrderTax.TaxRate.Name;
                return "Shipping";
            }
        }
    }

    public partial class tblTransaction
    {
        public IEnumerable<tblTransactionDetail> LocalTransactionDetails(SalesManagerEntities context)
        {
            return from c in context.tblTransactionDetail.Local
                   where c.Transaction == this
                   select c;
        }
    }

    public partial class tblTransactionDetail 
    {
        public decimal Debit
        {
            get { return (TransactionDetailTypeCode == "D") ? Amount : 0; }
        }

        public decimal Credit
        {
            get { return (TransactionDetailTypeCode == "C") ? Amount : 0; }
        }
    }

    public partial class tblAccount 
    {
        public string DisplayName
        {
            get
            {
                if (Name != null)
                    return Name;
                if (CardNumber != null)
                    return CardNumber.ToString();
                return BankAccountNumber + " " + BankRoutingNumber;
            }
        }
    }
}
